package mysql

import (
	"gorm.io/gorm"
	"video-hub/videorpc/videorpc"
)

type Comment struct {
	gorm.Model
	VideoId       int    `gorm:"type:int(11);comment:视频Id"`
	UserId        int    `gorm:"type:int(11);comment:用户Id"`
	Content       string `gorm:"type:varchar(500);comment:评论内容"`
	IsAnonymous   int    `gorm:"type:int(11);comment:是否匿名"`
	CommentTime   string `gorm:"type:varchar(30);comment:评论时间"`
	CommentStatus int    `gorm:"type:int(11);comment:评论状态"`
	ReplyId       int    `gorm:"type:int(11);comment:回复Id"`
}

func (c *Comment) ContentAdd(db *gorm.DB) error {
	return db.Debug().Create(c).Error
}

func (c *Comment) CommentDel(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).Delete(c).Error
}
func ContentList(db *gorm.DB, in *videorpc.ContentListRequest) ([]*videorpc.ContentList, error) {
	var list []*videorpc.ContentList
	//SELECT
	//  nick_name,
	//  content,
	//  comments.created_at
	//FROM
	//  comments
	//  LEFT JOIN users ON comments.user_id = users.id
	db.Debug().Model(&Comment{}).Select(
		"nick_name",
		"content",
		"comments.created_at",
	).Joins("LEFT JOIN users ON comments.user_id = users.id").Order("comments.created_at desc").
		Scopes(Paginate(int(in.Page), int(in.Size))).Find(&list)
	return list, err
}
